package com.leslie.app.service.impl;

import com.leslie.app.dto.ArticleDto;
import com.leslie.app.entity.Article;
import com.leslie.app.entity.User;
import com.leslie.app.mapper.ArticleMapper;
import com.leslie.app.mapper.UserMapper;
import com.leslie.app.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    ArticleMapper articleMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public List<Article> getArticleList(){
        return articleMapper.list();
    }

    @Override
    public void add(Article article) {
        articleMapper.add(article);
    }

    @Override
    public ArticleDto getArticleById(int id) {
        ArticleDto articleDto = new ArticleDto();
        Article article = articleMapper.getArticleById(id);
        articleDto.setId(article.getId());
        articleDto.setTitle(article.getTitle());
        articleDto.setContent(article.getContent());
        articleDto.setCoverImg(article.getCoverImg());
        articleDto.setState(article.getState());
        articleDto.setCategoryId(article.getCategoryId());
        // get user by user id
        User user = userMapper.findById(article.getCreateUser());
        articleDto.setCreateUser(user);
        articleDto.setCreateTime(String.valueOf(article.getCreateTime()));
        articleDto.setUpdateTime(String.valueOf(article.getUpdateTime()));
        return articleDto;
    }
}
